package com.booksmanager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booksmanager.annotation.Log;
import com.booksmanager.pojo.Books;
import com.booksmanager.service.BooksService;
import com.booksmanager.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 图书表 前端控制器
 * </p>
 *
 * @author Yan
 * @since 2020-05-24
 */
@RestController
@RequestMapping("/books")
@Api(value = "/books", tags = "图书接口模块")
public class BooksController {
    @Autowired
    private BooksService booksService;

    /**
     * 查询未上锁的books
     * @param page
     * @param books
     * @return
     */
    @ApiOperation(value = "分页查询未上锁的books")
    @GetMapping("/page")
    public R page(Page page, Books books){
        QueryWrapper<Books> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag","0");
        return new R(booksService.page(page,wrapper));
    }


    /**
     * 新增
     * @param books
     * @return
     */
    @ApiOperation(value = "新增图书")
    @Log("新增图书")
    @PostMapping("/save")
    public R save(@RequestBody Books books){
        return new R(booksService.save(books));
    }

    /**
     * 更新
     * @param books
     * @return
     */
    @ApiOperation(value = "更新图书")
    @PutMapping("/update")
    public R update(@RequestBody Books books){
        return new R(booksService.updateById(books));
    }


    /**
     * 逻辑删除图书
     * @param books
     * @return
     */
    @ApiOperation(value = "逻辑删除图书")
    @Log("逻辑删除图书")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Books books){
        books.setDelFlag("-1");
        return new R(booksService.updateById(books));
    }

}

